#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "Class.h"
#include "ProgEnv.h"
#include "Window.h"
#include "Scroller.h"
#include "ImageItem.h"
#include "String.h"

#include "CollTblView.h"
#include "Collection.h"

#include "Dictionary.h"
#include "OrdColl.h"

//---- PeDefaultViews ----------------------------------------------------------

class PeDefaultViews: public ObjectViewPolicy {
	Window *collWindow, *dictWindow, *bitmapWindow;
	PeCollTableView *collView;
	PeDictionaryView *dictView;
	ImageItem *bitmap;
public:
	MetaDef(PeDefaultViews);
	PeDefaultViews();
	~PeDefaultViews();
	char *ViewName(Class *cl, void *addr, Object *op);
	bool ShowView(Class *cl, void *addr, Object *op, Object *);
	void ShowVObjectTree(VObject*);
	void ShowDictionaryView(Dictionary*);
	void ShowCollectionView(Collection*);
	void ShowBitmap(Bitmap *bm);
};

NewMetaImpl(PeDefaultViews, ObjectViewPolicy, (TP(collWindow), TP(dictWindow), TP(bitmapWindow),
									TP(collView), TP(dictView), TP(bitmap)));

PeDefaultViews::PeDefaultViews()
{
	collWindow= dictWindow= bitmapWindow =0;
	bitmap= new ImageItem(new Bitmap(gPoint1, 1), 0, TRUE);
}

PeDefaultViews::~PeDefaultViews()
{
	SafeDelete(bitmap);
}

char *PeDefaultViews::ViewName(Class *cl, void *, Object *op)
{
	if (!op)
		return 0;
	if (op->IsKindOf(ImageItem))
		return "Bitmap View";
	if (op->IsKindOf(VObject))
		return "VObject-Tree View";
	if (op->IsKindOf(Dictionary) && ((Dictionary*)op)->Size())
		return "Dictionary View";
	if (op->IsKindOf(Collection) && ((Collection*)op)->Size())
		return "Collection View";
	if (cl && strcmp(cl->Name(), "Bitmap") == 0)
		return "Bitmap View";
	return 0;
}

bool PeDefaultViews::ShowView(Class *cl, void *addr, Object *op, Object *)
{
	if (op->IsKindOf(ImageItem)) {
		ImageItem *im= (ImageItem*)addr;
		if (im->GetBitmap()) {
			ShowBitmap(im->GetBitmap());
			return TRUE;
		}
	}
	if (op->IsKindOf(VObject)) {
		ShowVObjectTree((VObject*)op);
		return TRUE;
	}
	if (op->IsKindOf(Dictionary)) {
		ShowDictionaryView((Dictionary*)op);
		return TRUE;
	}
	if (op->IsKindOf(Collection)) {
		ShowCollectionView((Collection*)op);
		return TRUE;
	} else if (cl && strcmp(cl->Name(), "Bitmap") == 0) {
		ShowBitmap((Bitmap*)addr);
		return TRUE;
	}
	return FALSE;
}

void PeDefaultViews::ShowVObjectTree(VObject*vop)
{
	VObject *root= 0;
	while (vop) {
		root= vop;
		vop= vop->GetContainer();
	}
	if (root)
		gProgEnv->ShowInObjStruBrowser(root);
}

void PeDefaultViews::ShowCollectionView(Collection* col)
{
	if (collWindow == 0) {
		collView= new PeCollTableView();
		collWindow= new Window(0, Point(400, 300), eWinCanClose,
													new Scroller(collView));
	}
	collWindow->SetTitle(form("Collection: 0x%x (%s)", (int)col, col->ClassName()));
	collWindow->OpenAt(Point(800,500));
	collView->ShowCollection(col);
}

void PeDefaultViews::ShowDictionaryView(Dictionary* col)
{
	if (dictWindow == 0) {
		dictView= new PeDictionaryView();
		dictWindow= new Window(0, Point(600, 100), eWinCanClose, new Scroller(dictView));
	}
	dictWindow->SetTitle(form("Dictionary: 0x%x (%s)", (int)col, col->ClassName()));
	dictWindow->OpenAt(Point(800,500));
	dictView->ShowDictionary(col);
}

void PeDefaultViews::ShowBitmap(Bitmap *bm)
{
	Bitmap *bm2= (Bitmap*)bm->DeepClone();
	if (bitmapWindow == 0)
		bitmapWindow= new Window(0, Point(250, 200), eWinCanClose, new Scroller(bitmap));
	bitmapWindow->SetTitle(form("Bitmap: w: %d h: %d d: %d",
								bm2->Size().x, bm2->Size().y, bm2->GetDepth()));
	bitmapWindow->OpenAt(Point(800,500));
	bitmap->SetBitmap(bm2);
	bitmapWindow->UpdateEvent();
}

void ObjectViewOnEntry(ProgEnv *pe)
{
	pe->AddObjectViewPolicy(new PeDefaultViews);
}

